import axios from 'axios'
import { start, done } from 'nprogress'  //引入请求进度条
import 'nprogress/nprogress.css'//引入请求进度条样式


const service = axios.create({
  baseURL: '/api', //配置基础url
  timeout: 3000  //配置响应时间
})

//请求拦截器
service.interceptors.request.use(
  config => {
    start() //进度条开始
    return config
  },
  error => Promise.reject(error))

//响应拦截器
service.interceptors.response.use(
  response => {
    done()// 进度条结束
    if (response.status == 200) {
      const res = response.data
      if (res.code == 200 || res.code == 20000) {
        return res.data
      }
      return Promise.reject(new Error(res.message || 'Error'))
    }
    return Promise.reject(new Error(response.statusText || 'Error'))
  },
  error => Promise.reject(error))

export default service